package com.lln.cjcx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.lln.cjcx.pojo.Student;
import com.lln.cjcx.pojo.grade.Grade;
import com.lln.cjcx.pojo.grade.TermGrade;
import com.lln.cjcx.service.GradeExportService;
import com.lln.cjcx.style.SimpleStyleStrategy;
import com.lln.cjcx.style.WidthStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiLinnan
 * @version 1.0
 * @date 2020/1/29 20:33
 */

@Service
public final class GradeExportServiceImpl extends GradeExportService {
	public GradeExportServiceImpl(){
		super();
	}
	/**
	 * 按列导出查询结果
	 *
	 * @param grades  成绩列表
	 * @param name    名称前缀
	 * @param student 学生
	 * @return 文件名称
	 */

	@Override
	public final String gradeExportCol(List<TermGrade> grades, String name, Student student) {
		File file = new File(getFileName(name, student));
		ExcelWriter excelWriter = EasyExcel.write(file).build();
		int i = 0;
		for (TermGrade termGrade : grades) {
			WriteSheet writeSheet = EasyExcel.writerSheet(i++, termGrade.getTermName())
					.head(getColHead(termGrade))
					.registerWriteHandler(getCenterStyle())
					.registerWriteHandler(new WidthStrategy())
					.build();
			excelWriter.write(getColData(termGrade, student), writeSheet);
		}
		excelWriter.finish();
		return file.getName();
	}

	/**
	 * 按行导出查询结果
	 *
	 * @param grades  成绩列表
	 * @param name    名称前缀
	 * @param student 学生
	 * @return 文件名称
	 */
	@Override
	public String gradeExportRow(List<TermGrade> grades, String name, Student student) {
		File file = new File(getFileName(name, student));
		ExcelWriter excelWriter = EasyExcel.write(file)
				.build();
		int i = 0;
		for (TermGrade termGrade : grades) {
			WriteSheet writeSheet = EasyExcel.writerSheet(i++, termGrade.getTermName())
					.registerWriteHandler(getCenterStyle())
					.registerWriteHandler(new WidthStrategy())
					.head(getRowHead(termGrade))
					.build();
			excelWriter.write(getRowData(termGrade, student), writeSheet);
		}
		excelWriter.finish();
		return file.getName();
	}

	private String getFileName(String name, Student student) {
		return name + "/" + student.getNumber() + "_" + student.getName() + "_" + System.currentTimeMillis() + ".xlsx";
	}

	private SimpleStyleStrategy getCenterStyle() {
		WriteCellStyle writeCellStyle = new WriteCellStyle();
		writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
		writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		return new SimpleStyleStrategy(writeCellStyle);
	}

	private List<List<Object>> getColData(TermGrade termGrade, Student student) {
		List<List<Object>> lists = new ArrayList<>();
		ArrayList<Object> data = new ArrayList<>();

		data.add(student.getNumber());
		data.add(student.getName());
		List<Grade> grades = termGrade.getGrades();

		double sum = 0.0;
		int num = 0;
		for (Grade grade : grades) {
			String scoreString = grade.getScore();
			try {
				double score = Double.parseDouble(scoreString);
				if (score >= 0) {
					sum += score;
					num++;
					data.add(score);
					continue;
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			data.add("暂未录入");
		}
		data.add(sum);
		data.add(num == 0 ? "NaN" : sum / num);
		lists.add(data);
		return lists;
	}

	private List<List<String>> getColHead(TermGrade termGrade) {
		List<List<String>> lists = new ArrayList<>();
		ArrayList<String> head = new ArrayList<>();
		head.add("学号");
		lists.add(head);
		head = new ArrayList<>();
		head.add("姓名");
		lists.add(head);
		List<Grade> grades = termGrade.getGrades();
		for (Grade grade : grades) {
			head = new ArrayList<>();
			head.add(grade.getCourseName());
			lists.add(head);
		}
		head = new ArrayList<>();
		head.add("总分(有成绩科目)");
		lists.add(head);
		head = new ArrayList<>();
		head.add("平均分(有成绩科目)");
		lists.add(head);
		return lists;
	}

	private List<List<String>> getRowHead(TermGrade termGrade) {
		List<List<String>> lists = new ArrayList<>();
		ArrayList<String> head = new ArrayList<>();
		head.add("科目名称");
		lists.add(head);

		head = new ArrayList<>();
		head.add("最终成绩");
		lists.add(head);
		return lists;
	}


	private List<List<Object>> getRowData(TermGrade termGrade, Student student) {
		List<List<Object>> lists = new ArrayList<>();
		List<Grade> grades = termGrade.getGrades();
		double sum = 0.0;
		int num = 0;
		for (Grade grade : grades) {
			ArrayList<Object> row = new ArrayList<>();
			row.add(grade.getCourseName());
			String scoreString = grade.getScore();
			try {
				double score = Double.parseDouble(scoreString);
				if (score >= 0) {
					sum += score;
					num++;
					row.add(score);
					lists.add(row);
					continue;
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			row.add("暂未录入");
			lists.add(row);
		}
		ArrayList<Object> all = new ArrayList<>();
		all.add("总分(有成绩科目)");
		all.add(sum);
		lists.add(all);
		ArrayList<Object> avg = new ArrayList<>();
		avg.add("平均分(有成绩科目)");
		avg.add(num == 0 ? "NaN" : sum / num);
		lists.add(avg);
		return lists;
	}


	@Override
	public String getServName() {
		return "成绩导出服务";
	}
}
